// pages/jobExpectation/jobExpectation.js
import { request } from "../../utils/index.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeList: [
      { name: '短期兼职', value: 1 ,checked:'false' },
      { name: '长期兼职', value: 2 ,checked:'false'},
      { name: '周末兼职', value: 3 ,checked:'false'},
      { name: '实习', value: 4,checked:'false' }
    ],
    timeList: [
      { name: '工作日', value: 1 ,checked:'false' },
      { name: '周末', value: 2 ,checked:'false'},
      { name: '节假日', value: 3 ,checked:'false'},
      { name: '寒暑假', value: 4,checked:'false' }
    ],
    joinList: ['均可','每周一天','每周二天','每周三天','每周四天','每周五天','每周六天'],
    fulltimeList: [
      { name: '是', value: 1 ,checked:'false' },
      { name: '否', value: 0 ,checked:'false'}
    ],
    show: false,
    joinText:"",
    type:1, // 工作类型
    period:1, // 工作时间
    join:0, // 可上班时间
    is_fulltime:1, // 是否全职上班
  },

  // 点击工作类型
  onChange(e) {
    console.log(e)
    var that = this;
    let typeList = this.data.typeList;
    for (var i = 0; i < typeList.length; ++i) {
      if(typeList[i].value==e.detail[0]){
        typeList[i].checked=true;
      }else{
        typeList[i].checked = false;
      }
    }
    that.setData({typeList:typeList,type:e.detail[0]})
  },

    // 点击工作时间
    onChange1(e) {
      console.log(e)
      var that = this;
      let timeList = this.data.timeList;
      for (var i = 0; i < timeList.length; ++i) {
        if(timeList[i].value==e.detail[0]){
          timeList[i].checked=true;
        }else{
          timeList[i].checked = false;
        }
      }
      that.setData({timeList:timeList,period:e.detail[0]})
    },

  // 点击是否支持全职上班
  onChange2(e) {
    console.log(e)
    var that = this;
    let fulltimeList = this.data.fulltimeList;
    for (var i = 0; i < fulltimeList.length; ++i) {
      if(fulltimeList[i].value==e.detail[0]){
        fulltimeList[i].checked=true;
      }else{
        fulltimeList[i].checked = false;
      }
    }
    that.setData({fulltimeList:fulltimeList,is_fulltime:e.detail[0]})
  },


  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },

  onConfirm(event) {
    const { value, index } = event.detail;
    console.log(`当前值：${value}, 当前索引：${index}`)
    this.setData({
      show:false,
      join:index,
      joinText:value
    })

  },


  // 点击保存
  onSubmit(){
    var that = this; 
    request({ url: "/api/resume/update",data:{
      type:that.data.type,
      period:that.data.period,
      join:that.data.join,
      is_fulltime:that.data.is_fulltime
    }})
    .then(result => {
      console.log(result)
      wx.showToast({
        title: result.msg,
        icon: 'none',
        duration:2000
      });
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (options.jsonStr) {
      let item=JSON.parse(decodeURIComponent(options.jsonStr));
      this.setData({
        type:item.type,
        period:item.period,
        join:item.join,
        is_fulltime:item.is_fulltime,
        joinText:item.join==0?'均可':item.join==1?'每周一天':item.join==2?'每周二天':item.join==3?'每周三天':item.join==4?'每周四天':item.join==5?'每周五天':item.join==6?'每周六天':"",
      })
      var that = this; 
      let typeList = that.data.typeList;
      for (var i = 0; i < typeList.length; ++i) {
        if(typeList[i].value==item.type){
          typeList[i].checked=true;
        }else{
          typeList[i].checked = false;
        }
      }
      that.setData({typeList:typeList})
      let timeList = that.data.timeList;
      for (var i = 0; i < timeList.length; ++i) {
        if(timeList[i].value==item.period){
          timeList[i].checked=true;
        }else{
          timeList[i].checked = false;
        }
      }
      that.setData({timeList:timeList})
      let fulltimeList = that.data.fulltimeList;
      for (var i = 0; i < fulltimeList.length; ++i) {
        if(fulltimeList[i].value==item.is_fulltime){
          fulltimeList[i].checked=true;
        }else{
          fulltimeList[i].checked = false;
        }
      }
      that.setData({fulltimeList:fulltimeList})
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})